package org.test.client.ui.content.table;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import org.test.client.ui.component.paging.PagingBar;
import org.test.client.ui.component.panel.TitlePanePanel;
import org.test.client.ui.content.table.bstable.BsTable;
import org.test.client.ui.content.table.bstable.ColorfulInfo;
import org.test.client.ui.util.VFlowLayout;

public class TablePanel extends TitlePanePanel{

	public TablePanel() {
		super("Simple Table", null);
		doInit();
	}
	
	
	@Override
	public TitlePanePanel doInit() {
		super.doInit();
		
		String[] cols = new String[]{"Username","Date registered","Role",	"Status"};
		BsTable table = new BsTable();
		DefaultTableModel dtm = new DefaultTableModel(cols,0);
		table.setModel(dtm);
		table.setBorder(null);
		
		addData(dtm);
		
		PagingBar pb1 = new PagingBar(1,1000000);
		pb1.setPreferredSize(new Dimension(0,24));
		
		PagingBar pb2 = new PagingBar(1,1000000);
		pb2.setPreferredSize(new Dimension(0, 44));
		
		PagingBar pb3 = new PagingBar(1,1000000);
		pb3.setPreferredSize(new Dimension(0, 70));
		
		JPanel pagingPanel = new JPanel(new VFlowLayout(5));
		pagingPanel.add(pb1);
		pagingPanel.add(pb2);
		pagingPanel.add(pb3);
		pagingPanel.setPreferredSize(new Dimension(0,170));
		
		this.setLayout(new BorderLayout());
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(table);
		//消除 srcollpane 的边框
		scrollPane.setBorder(new EmptyBorder(4, 4, 4, 4));
		
		this.add(scrollPane);
		this.add(pagingPanel,BorderLayout.SOUTH);
		return this;
	}


	private void addData(DefaultTableModel dtm) {
		Object[][] rows = new Object[][] {
				{ "Muhammad Usman ", "2012/01/01 ", "Member ", new ColorfulInfo("Active" ,ColorfulInfo.TYPE_DEFAULT)},
				{ "White Horse ", "2012/02/01", "Staff", new ColorfulInfo("Banned" ,ColorfulInfo.TYPE_SUCCESS) },
				{ "Sheikh Heera", "2012/02/01", "Admin",new ColorfulInfo("Inactive" ,ColorfulInfo.TYPE_WARNING) },
				{ "Saruar", "2012/03/01", "Member", new ColorfulInfo("Pending" ,ColorfulInfo.TYPE_IMPORTANT)  },
				{ "Sana Amrin", "2012/01/21", "Staff ", new ColorfulInfo("Active" ,ColorfulInfo.TYPE_INFO) } 
			};
		
		int row = rows.length;
		int col = rows[0].length;
		for(int i=0;i<row;i++){
			dtm.addRow(rows[i]);
		}

		
		for(int i=0;i<100;i++){
			int random = (int) (Math.random()*5);
			
			Object[] testRow = { "Muhammad Usman"+i, "2012/01/01"+i, "Member"+i, 
					new ColorfulInfo("Active"+i,random )};
			
			dtm.addRow(testRow);
		}
	}
	
	
//	public static void main(String[] args) {
//		String[][] rows = new String[][] {
//				{ "Muhammad Usman ", "2012/01/01 ", "Member ", "Active " },
//				{ "White Horse ", " 2012/02/01", " 	Staff", "Banned " },
//				{ " Sheikh Heera", "2012/02/01 ", " Admin", "Inactive " },
//				{ " Saruar", "	2012/03/01 ", " Member", " Pending" },
//				{ " Sana Amrin", "2012/01/21 ", "	Staff ", "Active " } 
//			};
//		
//		System.out.println(rows.length + "row");
//		System.out.println(rows[0].length+"col");
//	}
}
